package com.example.iqiyitest.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.iqiyitest.utils.Video;
import com.example.iqiyitest.view.CustomGridLayoutManager;
import com.example.iqiyitest.view.MovieView;
import com.example.iqiyitest.view.SmoothRecyclerView;
import com.iqiyi.movieksdk.sdk.exported.LogUtils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.DefaultItemAnimator;
import android.view.View;
import android.widget.TextView;

@SuppressLint("HandlerLeak")
public class RecyclerBaseActivity extends Activity {
	protected SmoothRecyclerView rv_movies;
	protected CustomGridLayoutManager customGridLayoutManager;
	protected MovieView foucedView;
	protected int columnNum = 5;
	protected Map<String, String> queryPara = new HashMap<String, String>();
	protected TextView tv_index;
	protected TextView tv_count;
	protected List<Video> videos = new ArrayList<Video>();
	protected boolean isMovieViewInMaxLeft = false;
	protected int total = 0;
	protected int pageSize = 30;
	protected int hasLoad = 0;

	public void setFoucedView(MovieView foucedView) {
		this.foucedView = foucedView;
	}

	private Handler foucedDownHandler = new Handler() {
		public void handleMessage(Message msg) {
			View v = customGridLayoutManager.getNextDownView(rv_movies.getFocusedChild());
			if (v != null) {
				v.requestFocus();
			}
		};
	};

	protected void initRecyclerView() {
		customGridLayoutManager = new CustomGridLayoutManager(this, columnNum);
		customGridLayoutManager.setLastLineVisibleSearchDown(new CustomGridLayoutManager.OnLastLineVisibleSearchDown() {

			@Override
			public void searchDown() {
				rv_movies.smoothScrollY(rv_movies.getHeight() / 2);
				foucedDownHandler.sendEmptyMessageDelayed(0, 50);
			}
		});
		rv_movies.setLayoutManager(customGridLayoutManager);
		rv_movies.setHasFixedSize(true);
		rv_movies.setItemAnimator(new DefaultItemAnimator());
		rv_movies.setOnEndMovie(new SmoothRecyclerView.OnEndMovie() {

			@Override
			public void endMovie() {
				
			}
		});
	}
	
	protected void loadPage(final int index){
		if(hasLoad>index||index>=total){
			return;
		}
		LogUtils.d("#######加载第"+(index/pageSize+1)+"页");
		queryPara.put("order", "update");
		int currPage = index/pageSize+1;
		queryPara.put("pageNo", String.valueOf(currPage));
//		HttpClient.post(getVideosUrl, queryPara, new HttpClient.CallBack() {
//			@Override
//			public void onSuccess(String json) {
//				DataPage dataPage = HttpClient.gson.fromJson(json, DataPage.class);
//				List<Video> list = dataPage.getList();
//				int pageNo = dataPage.getPageNo();
//				int pageSize = dataPage.getPageSize();
//				if(hasLoad<pageNo * pageSize){
//					hasLoad = pageNo * pageSize;
//				}
//				total = dataPage.getTotal();
//				tv_count.setText(String.valueOf(total));
//				int before = (pageNo-1)*pageSize;
//				for (int i = 0; i < list.size(); i++) {
//					videos.set(i+before, list.get(i));
//				}
//				List<MovieView> displayedViews = customGridLayoutManager.getDisplayedViews();
//				if(displayedViews!=null&&!displayedViews.isEmpty()){	
//					for (MovieView movieView : displayedViews) {
//						int index = movieView.getIndex();
//						if(videos.size()>index){
//							movieView.setMovie(videos.get(index));
//						}
//					}
//				}
//			}
//
//			@Override
//			public void onFailure(String msg) {
//				LogUtils.e(Constants.TAG, msg);
//			}
//		});
		
	}
	
}
